package com.ljx.recruitment.mq.consumer.interview;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.ljx.recruitment.interview.constant.JobConstant;
import com.ljx.recruitment.interview.domain.dto.ResumeItmDto;
import com.ljx.recruitment.interview.domain.entity.Deliver;
import com.ljx.recruitment.interview.domain.entity.Job;
import com.ljx.recruitment.interview.service.DeliverService;
import com.ljx.recruitment.interview.service.JobService;
import com.ljx.recruitment.mq.constant.RabbitMqConstant;
import com.ljx.recruitment.mq.dto.DeliverEmailObjDto;
import com.ljx.recruitment.user.cache.UserCache;
import com.ljx.recruitment.user.domain.entity.Tenant;
import com.ljx.recruitment.user.domain.entity.User;
import com.ljx.recruitment.user.domain.vo.req.EmailSendReq;
import com.ljx.recruitment.user.service.EmailService;
import com.ljx.recruitment.user.service.TenantService;
import com.ljx.recruitment.user.service.UserService;
import com.ljx.recruitment.user.typehandler.details.ResumeDetails;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-11-05  22:41
 */
@Component
public class EmailConsumer {

    @Autowired
    private EmailService emailService;

    @Autowired
    private DeliverService deliverService;

    @Autowired
    private UserService userService;

    @Autowired
    private JobService jobService;

    @Autowired
    private TenantService tenantService;

    @Autowired
    private UserCache userCache;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = RabbitMqConstant.EMAIL_QUEUE, durable = "true"),
            exchange = @Exchange(name = RabbitMqConstant.EMAIL_EXCHANGE, type = ExchangeTypes.TOPIC),
            key = RabbitMqConstant.EMAIL_KEY
    ))
    public void listen(DeliverEmailObjDto dto){
        Long deliverId = dto.getDeliverId();
        Deliver deliver = deliverService.getById(deliverId);
        Long uid = deliver.getUId();
        User user = userCache.get(uid);
        Job job = jobService.getById(deliver.getJobId());
        Tenant tenant = tenantService.getById(user.getTenantId());

        Map<String, Object> resumeMap = getResumeMap(user.getResumeDetails());

        EmailSendReq emailSendReq = new EmailSendReq();
        fillEmailSendReqParams(dto, emailSendReq, resumeMap, job, tenant);
        emailService.doSendEmail(emailSendReq);
    }

    private void fillEmailSendReqParams(DeliverEmailObjDto dto, EmailSendReq emailSendReq,
                                        Map<String, Object> resumeMap, Job job, Tenant tenant) {
        Map<String, String> map = dto.getParamsMap();
        map.put("option", String.valueOf(dto.getOption()));
        map.put("name", resumeMap.get("name").toString());
        map.put("job", job.getName());
        map.put("tenantName", tenant.getName());
        emailSendReq.setType(dto.getEmailType());
        emailSendReq.setDetailsMap(map);
        emailSendReq.setTo(resumeMap.get("email").toString());
    }

    private Map<String, Object> getResumeMap(ResumeDetails userResumeDetails) {
        Map<String, Object> map = new HashMap<>();
        for (ResumeItmDto resumeItmDto : userResumeDetails) {
            map.put(resumeItmDto.getCode(), resumeItmDto.getDefaultValue());
            if (CollectionUtil.isNotEmpty(resumeItmDto.getChildList())){
                for (ResumeItmDto cItem : resumeItmDto.getChildList()) {
                    map.put(cItem.getCode(), cItem.getDefaultValue());
                }
            }
        }
        return map;
    }


}
